package com.hulk.dryad.admin.control;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hulk.dryad.admin.api.vo.DictCodeVO;
import com.hulk.dryad.admin.api.vo.DictTypeVO;
import com.hulk.dryad.admin.mapstruct.DictCodeStruct;
import com.hulk.dryad.admin.mapstruct.DictTypeStruct;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.constant.CacheConstants;
import com.hulk.dryad.persistence.entity.DictCode;
import com.hulk.dryad.persistence.entity.DictType;
import com.hulk.dryad.persistence.service.DictCodeService;
import com.hulk.dryad.persistence.service.DictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author kim
 * @since 2019-03-19
 */
@ApiSort(10)
@Api(value = "dict", tags = "字典管理模块")
@RestController
@AllArgsConstructor
@RequestMapping("/admin/dict")
public class SysDictController {

	private final DictTypeService sysDictService;

	private final DictCodeService sysDictItemService;

	/**
	 * 通过ID查询字典信息
	 * @param id ID
	 * @return 字典信息
	 */
	@ApiOperation(value = "getDictById", notes = "通过ID查询字典信息")
	@PrintlnLog
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		return R.ok(sysDictService.getById(id));
	}

	/**
	 * 分页查询字典信息
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@ApiOperation(value = "getDictPage", notes = "分页查询字典信息")
	@PrintlnLog
	@GetMapping("/page")
	public R<IPage> getDictPage(Page page, DictTypeVO vo) {
		DictType model =DictTypeStruct.INSTANCE.to(vo);
		return R.ok(sysDictService.page(page, Wrappers.query(model)));
	}

	/**
	 * 通过字典类型查找字典
	 * @param type 类型
	 * @return 同类型字典
	 */
	@ApiOperation(value = "getDictByType", notes = "通过字典类型查找字典")
	@PrintlnLog
	@GetMapping("/type/{type}")
	@Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type", unless = "#result.data.isEmpty()")
	public R<List<DictCode>> getDictByType(@PathVariable String type) {
		return R.ok(sysDictItemService.list(Wrappers.<DictCode>query().lambda().eq(DictCode::getDictType, type)));
	}

	/**
	 * 添加字典
	 * @param vo 字典信息
	 * @return success、false
	 */
	@ApiOperation(value = "saveDict", notes = "添加字典")
	@Idempotent
	@PrintlnLog
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_dict_add')")
	public R save(@Valid @RequestBody DictTypeVO vo) {
		DictType model =DictTypeStruct.INSTANCE.to(vo);
		return R.ok(sysDictService.save(model));
	}

	/**
	 * 删除字典，并且清除字典缓存
	 * @param id ID
	 * @return R
	 */
	@ApiOperation(value = "removeDictById", notes = "删除字典")
	@PrintlnLog
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_dict_del')")
	public R removeById(@PathVariable Integer id) {
		return sysDictService.removeDictType(id);
	}

	/**
	 * 修改字典
	 * @param vo 字典信息
	 * @return success/false
	 */
	@ApiOperation(value = "updateDictById", notes = "修改字典")
	@Idempotent
	@PrintlnLog
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_dict_edit')")
	public R updateById(@Valid @RequestBody DictTypeVO vo) {
		DictType model =DictTypeStruct.INSTANCE.to(vo);
		return sysDictService.updateDictType(model);
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param vo 字典项
	 * @return
	 */
	@ApiOperation(value = "getSysDictItemPage", notes = "分页查询字典项")
	@PrintlnLog
	@GetMapping("/item/page")
	public R getSysDictItemPage(Page page, DictCodeVO vo) {
		DictCode model =	DictCodeStruct.INSTANCE.to(vo);
		return R.ok(sysDictItemService.page(page, Wrappers.query(model)));
	}

	/**
	 * 通过id查询字典项
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "getDictItemById", notes = "通过id查询字典项")
	@PrintlnLog
	@GetMapping("/item/{id}")
	public R getDictItemById(@PathVariable("id") Integer id) {
		return R.ok(sysDictItemService.getById(id));
	}

	/**
	 * 新增字典项
	 * @param vo 字典项
	 * @return R
	 */
	@ApiOperation(value = "saveDictItem", notes = "新增字典项")
	@Idempotent
	@PrintlnLog
	@PostMapping("/item")
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	public R save(@RequestBody DictCodeVO vo) {
		DictCode model =	DictCodeStruct.INSTANCE.to(vo);
		return R.ok(sysDictItemService.save(model));
	}

	/**
	 * 修改字典项
	 * @param vo 字典项
	 * @return R
	 */
	@ApiOperation(value = "updateDictCodeById", notes = "修改字典项")
	@Idempotent
	@PrintlnLog
	@PutMapping("/item")
	public R updateById(@RequestBody DictCodeVO vo) {
		DictCode model =	DictCodeStruct.INSTANCE.to(vo);
		return sysDictItemService.updateDictCode(model);
	}

	/**
	 * 通过id删除字典项
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "removeDictItemById", notes = "通过id删除字典项")
	@PrintlnLog
	@DeleteMapping("/item/{id}")
	public R removeDictItemById(@PathVariable Integer id) {
		return sysDictItemService.removeDictCode(id);
	}

}
